package com.example.teach.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.teach.entity.Clazz;
import com.example.teach.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ClassMapper extends BaseMapper<Clazz> {

    @Insert("insert into class(teacher_id,class_name,major_id,course_id,number) values(#{teacherId},#{className},#{majorId},#{courseId},#{number})")
    void create(Integer teacherId, String className,Integer majorId,Integer courseId,Integer number);

    @Select("select * from class where class_name=#{className}")
    Clazz selectByClassName(String className);

    @Select("select * from user_class where user_id=#{userId} and class_id=#{classId}")
    User selectByUserId(Integer userId, Integer classId);

    @Select("select number from class where id=#{classId}")
    int selectClassNumber(Integer classId);

    @Insert("insert into user_class(user_id,class_id) values(#{userId},#{classId})")
    boolean join(Integer userId, Integer classId);

    @Update("update class set number = number - 1 where id=#{classId}")
    void subtractOne(Integer classId);

    @Delete("delete from user_class where user_id=#{userId} and class_id=#{classId}")
    boolean quit(Integer userId, Integer classId);

    @Update("update class set number = number + 1 where id=#{classId}")
    void addOne(Integer classId);

    @Update("update class set course_id = #{courseId} where id=#{classId}")
    void updateCourseId(Integer classId,Integer courseId);

    @Update("update class set state = 1 where id=#{classId}")
    void remove(Integer classId);

    int updateClassNum(Integer id, Integer newExisting,Integer newRemaining, Integer version);

    @Update("update class set remaining = 1 - remaining where id=#{id}")
    void updateRemaining(Integer id);
}
